# coding: utf-8
from sqlalchemy import Column, String, Table ,DateTime
from sqlalchemy.dialects.mysql import INTEGER, VARCHAR 
from sqlalchemy.ext.declarative import declarative_base
from exts import db
import datetime
from sqlalchemy.orm import relationship, backref




class Active(db.Model):
    __tablename__ = 'active'
    __table_args__ = {'comment': '活动表'}

    activid = Column(VARCHAR(255), primary_key=True, comment='活动id')
    activityname = Column(VARCHAR(255), comment='一等奖')
    activetime = Column(DateTime)
    sponsorid = Column(VARCHAR(255))
    sponsorname = Column(VARCHAR(255))
    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            
            del dict["_sa_instance_state"]
        #时间转化  
        # dd = datetime.datetime(2020, 10, 15, 23, 26, 46).strftime('%Y-%m-%d %H:%M:%S') 
        # print(dd)   
            for a,b in dict.items():
                if isinstance(b, datetime.date) == True :
                    print(a,b)
                    dict.update({a : b.strftime('%Y-%m-%d %H:%M:%S')})
                    print(dict)
                else:
                    pass
        return dict
    
class Gift(db.Model):
    __tablename__ = 'gift'
    __table_args__ = {'comment': '礼物表'}
    
    giftnum = Column(INTEGER(255), comment='奖品份数')
    giftname = Column(VARCHAR(255))
    giftid = Column(VARCHAR(255), primary_key=True, comment='礼品id')
    activid = Column(INTEGER(11))
    winname = Column(VARCHAR(255))
    imagesurl = Column(VARCHAR(255))
     #转json函数
    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict
class Order(db.Model):
    __tablename__ = 'order'
    __table_args__ = {'comment': '发货表'}
    activid = Column(INTEGER(11))
    giftid = Column(INTEGER(11), primary_key=True, nullable=False, comment='唯一名单')
    userid = Column(INTEGER(11), comment='用户id')
    giftname = Column(VARCHAR(255), comment='礼品名称')
    address = Column(VARCHAR(255), comment='收货地址')
    username = Column(String(255))
    phone = Column(String(255))
    luckynum = Column(INTEGER(255), primary_key=True, nullable=False, comment='中奖号码')


class Ticket(db.Model):
    __tablename__ = 'ticket'
    activid = Column(VARCHAR(255))
    userid = Column(INTEGER(11), nullable=False)
    ticketnum = Column(INTEGER(11), primary_key=True, comment='彩票号码')
    iswin = Column(String(255), comment='是否中奖')
    giftid = Column(INTEGER(11), comment='奖品id')
    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict

class User(db.Model):
    __tablename__ = 'user'
  
    uuid = Column(INTEGER(11), primary_key=True, nullable=False)
    userid = Column(INTEGER(11), primary_key=True, nullable=False)
    username = Column(VARCHAR(255))
    password = Column(VARCHAR(255))
    avatar = Column(VARCHAR(255))
    address = Column(String(255))
    phone = Column(String(255))
    #转json函数
    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict

